﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>
#include <QTextStream>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initUi();
    connect(this->spinBox,SIGNAL(valueChanged(int)),this,SLOT(on_spinBox_valueChanged(int)));
    connect(this->fontComboBox,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(on_fontComboBox_changed(const QString &)));
    setWindowTitle("my_tool");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUi()
{
    fileName=new QLabel;
    fileName->setMinimumWidth(100);
    fileName->setText("当前文件: ");
    ui->statusbar->addWidget(fileName);



    progressBar=new QProgressBar;
    progressBar->setMinimum(6);
    progressBar->setMaximum(80);
    progressBar->setMinimumWidth(80);
    progressBar->setValue(ui->textEdit->fontPointSize());
    ui->statusbar->addWidget(progressBar);





    spinBox=new QSpinBox;
    spinBox->setMaximum(80);
    spinBox->setMinimum(6);
    spinBox->setMaximumWidth(200);
    spinBox->setMinimumWidth(80);
    spinBox->setValue(ui->textEdit->fontPointSize());
    QLabel *label=new QLabel("字体大小");
    ui->toolBar->addSeparator();
    ui->toolBar->addWidget(label);
    ui->toolBar->addWidget(spinBox);




    fontComboBox=new QFontComboBox;
    fontComboBox->setMinimumWidth(80);
    fontComboBox->setMaximumWidth(200);
    fontComboBox->setCurrentFont(ui->textEdit->font());
    ui->toolBar->addSeparator();
    QLabel *fontLabel=new QLabel("字体");
    ui->toolBar->addWidget(fontLabel);
    ui->toolBar->addWidget(fontComboBox);
}


void MainWindow::on_action_New_triggered()
{
    fileName->setText("当前文件: ");
    ui->textEdit->clear();
}

void MainWindow::on_action_Open_triggered()
{
    QString filename=QFileDialog::getOpenFileName(this,"打开一个文件");
    if(!filename.isEmpty())
    {
        QFile file(filename);
        QTextStream textstream(&file);
        if(file.open(QIODevice::Text|QIODevice::ReadWrite))
        {
            while(!textstream.atEnd())
            {
                ui->textEdit->append(textstream.readLine());
            }
        }
        fileName->setText("当前文件: "+filename);
    }
}

void MainWindow::on_action_Blod_triggered(bool checked)
{
    auto format=ui->textEdit->currentCharFormat();
    format.setFontWeight(checked?QFont::Bold:QFont::Normal);
    ui->textEdit->mergeCurrentCharFormat(format);
}

void MainWindow::on_action_italic_triggered(bool checked)
{
    auto format=ui->textEdit->currentCharFormat();
    format.setFontItalic(checked);
    ui->textEdit->mergeCurrentCharFormat(format);
}

void MainWindow::on_action_Underline_triggered(bool checked)
{
    auto format=ui->textEdit->currentCharFormat();
    format.setFontUnderline(checked);
    ui->textEdit->mergeCurrentCharFormat(format);
}

void MainWindow::on_spinBox_valueChanged(int size)
{
    QTextCharFormat format;
    format.setFontPointSize(size);
    ui->textEdit->mergeCurrentCharFormat(format);
    progressBar->setValue(size);

}

void MainWindow::on_fontComboBox_changed(const QString &fontName)
{
    QTextCharFormat format;
    format.setFontFamily(fontName);
    ui->textEdit->mergeCurrentCharFormat(format);
}

void MainWindow::on_textEdit_copyAvailable(bool b)
{
    ui->action_Cut->setEnabled(b);
    ui->action_Copy->setEnabled(b);
    ui->action_Paste->setEnabled(ui->textEdit->canPaste());
}

void MainWindow::on_textEdit_selectionChanged()
{
    QTextCharFormat format=ui->textEdit->currentCharFormat();
    ui->action_Blod->setChecked(format.font().bold());
    ui->action_italic->setChecked(format.fontItalic());
    ui->action_Underline->setChecked(format.fontUnderline());
}

void MainWindow::on_action_insert_triggered()
{
    QString str=QFileDialog::getOpenFileName(this,"选择一张图片");
    if(str.isEmpty())return;
    QString ret=QString::asprintf("<img src=%s>",str.toStdString().c_str());
    ui->textEdit->setHtml(ret);
}
